<?php
/**
 * OwnRaid Data Accessor for CTRT_RaidTracker
 *
 * @category Plugins
 * @package CT_RaidTrackerImport
 * @copyright (c) 2006, EQdkp <http://www.eqdkp.com>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 *
 * @author Garrett Hunter <loganfive@blacktower.com>
 * $Rev: 84 $ $Date: 2007-04-22 09:42:16 +0000 (Sun, 22 Apr 2007) $
 */

if ( !defined('EQDKP_INC') )
{
    die('Haxor!!!');
}

include_once('dataaccessor.php');

/**
 * CRUD Class OwnRaid table
 * @subpackage CTRTDataAccessors
 */
class CTRT_OwnRaid extends CTRT_DataAccessor {

	var $_own_raid_name;
	
	function CTRT_OwnRaid () { 
		$this->setMyTable(CTRT_OWN_RAIDS_TABLE);
		$this->setMyPrimaryKey("own_raid_id");
	}
	
	/**
	 * Update a record
	 * @var array $data Contains all values to be updated
	 */
	function update($data) {

		$this->setOwnRaidName($data['own_raid_name']);

		if (!$this->isDuplicate($data['own_raid_id'],$this->getOwnRaidName())) {

			$this->doUpdate ($data[$this->getMyPrimaryKey()],
			                 array('own_raid_name'=>stripslashes($this->getOwnRaidName()))
							);
		} else {
			return 0;
		}

		return 1;
	}

	/**
	 * Insert a record
	 * @var array $data Contains all column data to insert
	 */
	function insert($data) {
		global $db;

		$this->setOwnRaidName($data['own_raid_name']);

		if (!$this->isDuplicate(-1,$this->getOwnRaidName())) {
		
			$this->doInsert (array('own_raid_name'=>stripslashes($this->getOwnRaidName())));
		} else {
			return 0;
		}
		
		return 1;
	}

	/**
	 * Get one or more records
	 */
	function getRecord ($cmd=CTRT_GET_ALL,$param="") {
        global $db;

        /**
         * Generate the list of ownRaids
         */
        $sql =   "SELECT own_raid_id, own_raid_name
                    FROM " . $this->getMyTable() . " ";
		
		switch ($cmd) {
			case CTRT_GET_ALL:
				break;
			
			case CTRT_GET_BY_ID:
				$sql .= "WHERE own_raid_id = ".$param." ";
				break;
					
			case CTRT_GET_BY_NAME:
				$this->setOwnRaidName($param);
				$sql .= "WHERE own_raid_name = '".mysql_real_escape_string($this->getOwnRaidName())."' ";
				break;
			
			default: break;
		}
		$sql .= "ORDER BY own_raid_name";
		
        $result = $db->query($sql);

		$ownRaids = array();
        while ( $row = $db->fetch_record($result) )
        {
			$ownRaids[] = array("own_raid_id"=>$row["own_raid_id"],
			                         "own_raid_name"=>$row["own_raid_name"]
								    );
        }
        $db->free_result($result);
		
		switch ($cmd) {
			case CTRT_GET_ALL:
				break;
			
			case CTRT_GET_BY_NAME:
			case CTRT_GET_BY_ID:
				$ownRaids = $ownRaids[0];
				break;
			
			default: break;
		}
		
		return $ownRaids;
	}
	
	/**
	 * Checks a name / result combo against database for duplicate
	 * @var int $key primary key used to filter this record out
	 * @var string $name own raid name
	 */
	function isDuplicate($key, $name) {
		global $db;
		
		/**
		 * Only one name <-> result combination can exist
		 */ 
		$sql = "SELECT own_raid_id
		          FROM ".$this->getMyTable()."
				 WHERE own_raid_name = '".mysql_real_escape_string($name)."'
				   AND own_raid_id != ".$key;
		$record = $db->query_first($sql);
		if (isset($record)) {		
			return 1;
		} else {
			return 0;
		}
	}
	
	//========================================
	// Getter / Setter methods
	//========================================

	/**
	 * Get / Set own_raid_name
	 */
	function setOwnRaidName($name) { $this->_own_raid_name = $this->capitalize($name); return 1; }
	function getOwnRaidName() { return $this->_own_raid_name; }

}
?>